package com.ruoyi.report_hntjg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report_hntjg.domain.ReportHntjgHntqdhtfbgBaDetail;
import com.ruoyi.report_hntjg.domain.bo.ReportHntjgHntqdhtfjlbBsInfoBo;
import com.ruoyi.report_hntjg.mapper.ReportHntjgHntqdhtfjlbBaDetailMapper;
import com.ruoyi.report_hntjg.mapper.ReportHntjgHntqdhtfjlbBsInfoMapper;
import com.ruoyi.report_hntjg.service.IReportHntjgHntqdhtfjlbBsInfoService;
import com.ruoyi.report_hntjg.domain.ReportHntjgHntqdhtfjlbBaDetail;
import com.ruoyi.report_hntjg.domain.ReportHntjgHntqdhtfjlbBsInfo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgHntqdhtfjlbBaDetailVo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgHntqdhtfjlbBsInfoVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 混凝土强度试验检测记录（回弹法）Service业务层处理
 *
 * @author tll
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class ReportHntjgHntqdhtfjlbBsInfoServiceImpl implements IReportHntjgHntqdhtfjlbBsInfoService {

    private final ReportHntjgHntqdhtfjlbBsInfoMapper baseMapper;
    private final ReportHntjgHntqdhtfjlbBaDetailMapper reportHntjgHntqdhtfjlbBaDetailMapper;

    /**
     * 查询混凝土强度试验检测记录（回弹法）
     */
    @Override
    @FIllBasicInfo
    public ReportHntjgHntqdhtfjlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportHntjgHntqdhtfjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportHntjgHntqdhtfjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportHntjgHntqdhtfjlbBaDetail::getExcelId, excelId);
//        lqw.orderByAsc(ReportHntjgHntqdhtfjlbBaDetail::getCreateTime);
        lqw.orderByAsc(ReportHntjgHntqdhtfjlbBaDetail::getSort);
        List<ReportHntjgHntqdhtfjlbBaDetailVo> detailList = reportHntjgHntqdhtfjlbBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询混凝土强度试验检测记录（回弹法）列表
     */
    @Override
    public TableDataInfo<ReportHntjgHntqdhtfjlbBsInfoVo> queryPageList(ReportHntjgHntqdhtfjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportHntjgHntqdhtfjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportHntjgHntqdhtfjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土强度试验检测记录（回弹法）列表
     */
    @Override
    public List<ReportHntjgHntqdhtfjlbBsInfoVo> queryList(ReportHntjgHntqdhtfjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportHntjgHntqdhtfjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportHntjgHntqdhtfjlbBsInfo> buildQueryWrapper(ReportHntjgHntqdhtfjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportHntjgHntqdhtfjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportHntjgHntqdhtfjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportHntjgHntqdhtfjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportHntjgHntqdhtfjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportHntjgHntqdhtfjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportHntjgHntqdhtfjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportHntjgHntqdhtfjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportHntjgHntqdhtfjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportHntjgHntqdhtfjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportHntjgHntqdhtfjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportHntjgHntqdhtfjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportHntjgHntqdhtfjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportHntjgHntqdhtfjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportHntjgHntqdhtfjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportHntjgHntqdhtfjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportHntjgHntqdhtfjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportHntjgHntqdhtfjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportHntjgHntqdhtfjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportHntjgHntqdhtfjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportHntjgHntqdhtfjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportHntjgHntqdhtfjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportHntjgHntqdhtfjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportHntjgHntqdhtfjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportHntjgHntqdhtfjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportHntjgHntqdhtfjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportHntjgHntqdhtfjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportHntjgHntqdhtfjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportHntjgHntqdhtfjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增混凝土强度试验检测记录（回弹法）
     */
    @Override
    public Boolean insertByBo(ReportHntjgHntqdhtfjlbBsInfoBo bo) {
        ReportHntjgHntqdhtfjlbBsInfo add = BeanUtil.toBean(bo, ReportHntjgHntqdhtfjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            List<ReportHntjgHntqdhtfjlbBaDetail> detailList = new ArrayList<>(10);
            for (long i=1; i<=10L; i++) {
                detailList.add(new ReportHntjgHntqdhtfjlbBaDetail(bo.getExcelId(), i));
            }
            reportHntjgHntqdhtfjlbBaDetailMapper.insertBatch(detailList);
        }
        return flag;
    }

    /**
     * 修改混凝土强度试验检测记录（回弹法）
     */
    @Override
    public Boolean updateByBo(ReportHntjgHntqdhtfjlbBsInfoBo bo) {
        // 主表
        ReportHntjgHntqdhtfjlbBsInfo update = BeanUtil.toBean(bo, ReportHntjgHntqdhtfjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            reportHntjgHntqdhtfjlbBaDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportHntjgHntqdhtfjlbBaDetail.class));
        }
        return flag;

//        boolean upd = baseMapper.updateById(update) > 0;
//        // 子表
//        boolean upd2 = false;
//        boolean insert = false;
//        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportHntjgHntqdhtfjlbBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportHntjgHntqdhtfjlbBaDetail> list = reportHntjgHntqdhtfjlbBaDetailMapper.selectList(queryWrapper);
//            for (ReportHntjgHntqdhtfjlbBaDetail det : bo.getDetailList()) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = reportHntjgHntqdhtfjlbBaDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = reportHntjgHntqdhtfjlbBaDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportHntjgHntqdhtfjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土强度试验检测记录（回弹法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
